<?php

	/**
	 * Workspace@ Engine application comment!
	 * 
	 * LICENSE
	 * This application is licensed under New BSD license.
	 * Please read complete LICENSE.txt inside of root folder for more details.
	 * 
	 * We wish you good building :) 
	 * 
	 * @category   Application
	 * @package    Bootstrap
	 * @author     Nevio Vesic <nevio@workspaceat.com>
	 * @copyright  2008-2009 Workspace@ group in association with Design strategist
	 */

	class Engine_Plugins_Accounting extends Engine_Application_Abstract
	{
		
		protected $db          = null;

		/**
		 * Group id temporairly id ( used for ajax when adding group or modifying group )
		 * 
		 * @var int[group id]
		 */
		private static $groupid = null;
		
		/**
		 * Group real status. When group is building it will say inprogress.
		 * Later after group has been finished, than he will read this value
		 * and publish it or disable it.
		 * Start value is added at addGroup method
		 * 
		 * @var int[group id]
		 */
		private static $status  = null;
		
		function __construct()
		{
			
			$this->db = $this->databaseInstance();
		
		}
		
		public function addGroup( Zend_Config $data )
		{
			if (! $data -> name || strlen ( $data->name ) < 3 )
			{
				return array (
					"status" => "false",
					"input"  => "name",
					"message" => "Group name must be specified and grater than 3 chars!"
				);
				die();
			}
			
			if (! $data -> shortdesc || strlen ( $data->shortdesc ) < 10 )
			{
				return array (
					"status" => "false",
					"input"  => "shortdescriptions",
					"message" => "Short description must be specified and grater than 10 chars!"
				);
				die();
			}
			
			if (! $data -> status )
			{
				return array (
					"status" => "false",
					"input"  => "status",
					"message" => "You must choose status for current group!"
				);
				die();
			}

			if (! $data -> accept )
			{
				return array (
					"status" => "false",
					"input"  => "accept",
					"message" => "Group accept members must have some value! Choose from listed ones!"
				);
				die();
			}
			
			if (! $data -> description )
			{
				return array (
					"status" => "false",
					"input"  => "description",
					"message" => "Description must be specified and grather than 10 chars!"
				);
				die();
			}
			
			# lets get unix name
			$unixname = $this->replaceChars( $data -> name );
			
			$this->db->setFetchMode(Zend_Db::FETCH_OBJ);

			$result_pres = $this->db->fetchRow( "SELECT * FROM user_groups WHERE unixname='{$unixname}'");
			
			if ( $result_pres )
			{
				return array (
					"status" => "false",
					"input"  => "name",
					"next"   => "{$result_pres->progressnext}",
					"message" => "Current group allready exists! Please choose another one"
				);
				die();
			}else{
				
				# ok we have bypassed all requestices, now lets add first stage
				$result = $this->db->query("INSERT INTO user_groups ( garbaged, name, unixname, shortdescription, status, progressnext, acceptinvitations, description )
											VALUES ( 'false', '{$data->name}', '{$unixname}', '{$data->shortdesc}', 'inprogress', '2',
											          '{$data->accept}', '{$data->description}')");
											         
				if (! $result )
				{
					return array (
						"status" => "false",
						"input"  => "",
						"message" => "There was a sql error. Group can't be added!"
					);
					die();	
				}		
				
				$result_post = $this->db->fetchRow( "SELECT id FROM user_groups WHERE unixname = '{$unixname}'");
				
				
				# lets set temp id
				$this->setAddGroupId($result_post->id);
				
					return array (
						"status" => "true",
						"input"  => "",
						"task"   => "2",
						"message" => "About group has been successfuly saved! Processing..."
					);
					die();
					
			}
						
		}
		
		public function addGroupPermissions( Zend_Config $data )
		{
			
		}

		public function addGroupOwnership( Zend_Config $data )
		{
			
		}
		
		public function getAddedRow( Zend_Config $data )
		{
			
		}
		
		private function setAddGroupId( $id )
		{
			if ( self::$groupid )
			{
				self::$groupid = null;
			}
			
			self::$groupid = $id;
		}
		
		private function getAddGroupId( )
		{
			return self::$groupid;
		}
		
		
		/**
		 * Stupid function for replacing illegal characters with "" and white spaces with -
		 *
		 * @param  string $str
		 * @return string
		 */
		private function replaceChars($str)
		{
			$a = str_replace("`", "", $str );
			$b = str_replace("~", "", $a   );
			$c = str_replace("`", "", $b   );
			$d = str_replace("!", "", $c   );
			$e = str_replace("@", "", $d   );
			$f = str_replace("#", "", $e   );
			$g = str_replace("$", "", $f   );
			$h = str_replace("%", "", $g   );
			$i = str_replace("^", "", $h   );
			$j = str_replace("&", "", $i   );
			$k = str_replace("*", "", $j   );
			$l = str_replace("(", "", $k   );
			$m = str_replace(")", "", $l   );
			$n = str_replace("}", "", $m   );
			$o = str_replace("{", "", $n   );
			$p = str_replace("[", "", $o   );
			$r = str_replace("]", "", $p   );
			$s = str_replace(":", "", $r   );
			$t = str_replace("\\", "", $s   );
			$u = str_replace("/", "", $t   );
			$v = str_replace("'", "", $u   );
			$x = str_replace("\"", "", $v   );
			$y = str_replace("|", "", $x   );
			$z = str_replace("<", "", $y   );
			$a1 = str_replace(">", "", $z   );
			$a2 = str_replace(";", "", $a1   );
			$a3 = str_replace(" ", "-", $a2   );
			$a4 = str_replace("=", "", $a3   );
			$a5 = str_replace(",", "", $a4   );
			$a6 = str_replace(".", "_", $a5   );
			return $a6;
		}		
	}

